package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.ApiErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.rpcservice.DepotTransferService;
import com.cmc.cloud.cmclink.eqm.api.depot.DepotApi;
import com.cmc.cloud.cmclink.eqm.api.model.request.DepotQueryRequest;
import com.cmc.cloud.cmclink.eqm.api.model.response.DepotDTO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author heguo
 */
@Service
@Validated
@Slf4j
public class DepotTransferServiceImpl implements DepotTransferService {

    @Resource
    private DepotApi depotApi;

    @Override
    public List<DepotDTO> queryDepotByPortCode(String portCode, Boolean isCfs, Boolean enabled) {
        DepotQueryRequest depotQueryRequest = new DepotQueryRequest();
        depotQueryRequest.setPortCode(portCode);
        depotQueryRequest.setIsCfs(isCfs);
        depotQueryRequest.setEnabled(enabled);
        CommonResult<List<DepotDTO>> listCommonResult;
        try {
            listCommonResult = depotApi.pageByPortCode(depotQueryRequest);
            if (listCommonResult != null && listCommonResult.getCode() == 0) {
                return listCommonResult.getData();
            } else {
                throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.QUERY_DEPOT_FAILED);
            }
        } catch (Exception e) {
            log.error("查询堆场信息失败：" + e.getMessage(), e);
            throw ServiceExceptionUtil.exception(ApiErrorCodeConstants.QUERY_DEPOT_FAILED);
        }
    }
}
